package com.controller;

import com.dao.IStudentDAO;
import com.dao.IStudentDAOImpl;
import com.dao.IUserDAOImpl;
import com.pojo.Student;
import com.pojo.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @CreateTime: 2022-12-25  14:21
 * @Description: TODO
 * @Version: 1.0.0
 */
@WebServlet(value = "/user")
public class UserController extends HttpServlet {

    private IUserDAOImpl userDAO = new IUserDAOImpl();
    private IStudentDAOImpl studentDAO= new IStudentDAOImpl();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // super.doPost(req, resp);
        //  不能调用父类的方法，否则会报错
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = userDAO.findByUserName(username);
        if (user != null) {
            if (user.getPassword().equals(password)) {
        List<Student> allList = studentDAO.findAllList();
        req.setAttribute("allList", allList);
        req.getRequestDispatcher("list.jsp").forward(req, resp);
            } else {
                req.setAttribute("msg", "密码错误");
                req.getRequestDispatcher("/").forward(req, resp);
            }
        } else {
            req.setAttribute("msg", "用户名不存在");
            req.getRequestDispatcher("/").forward(req, resp);
        }
    }

}
